#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "oled.h"
#include "gui.h"
#include "bmp.h"
#include "base_type.h"
#include "oled_menu.h"
#include "button_module.h"
#if 1
extern int year;
extern int date_month;
extern int date_day;
extern int hour;
extern int min;
extern int sec;
extern int TEMP_WAITTIME;   //等待冷却的时间(s)
extern int oled_flag;
extern float TEMP_MAX;
extern float Temperature;
#endif

void Draw_Menu(void)        //基本界面
{
#if 1
    extern float Temperature;
    extern float Humidity;
    int a = 0;
    int b = 0;
    int c = 0;
    int d = 0;
    int e = 0;
    int f = 0;
    int g = 0;

    a = Temperature;
    b = a / 10;
    c = a % 10;
    d = (Temperature - a) * 10;
    
    e = Humidity;
    f = e / 10;
    g = e % 10;

    printf("OLED: Temperature:%f  Humidity:%f \r\n",Temperature,Humidity);
    //printf("Temp: b:%d c:%d d:%d Hum: f:%d g:%d\r\n", b, c, d, f, g);
    

#endif
    int month_shi = date_month/10;
    int month_ge = date_month%10;
    int day_shi = date_day/10;
    int day_ge = date_day%10;
    int hour_shi = hour/10;
    int hour_ge = hour%10;
    int min_shi = min/10;
    int min_ge = min%10;
    int sec_shi = sec/10;
    int sec_ge = sec%10;
    if(oled_flag < 4)
    {
        if(Temperature < TEMP_MAX)
        {
            //WIDTH = 128, HEIGHT = 64 最大边界不可用
            GUI_FillRectangle(39,11, WIDTH-1,19, 0);
            GUI_DrawLine(38, 10, 38, HEIGHT-1, 1);
            GUI_DrawLine(1, 10, WIDTH-1, 10, 1);
            GUI_DrawLine(38, 45, WIDTH-1, 45, 1);
            GUI_DrawLine(64, 45, 64, HEIGHT-1, 1);
            GUI_ShowString(42,52,"HUM",8,1);
            GUI_DrawCircle(WIDTH-1-19, 25, 1,2);
            GUI_ShowString(WIDTH-1-14,20,"C",16,1);

            //显示温湿度每位数据
            GUI_ShowNum(WIDTH/2,20,b,1,16,1);
            GUI_ShowNum(WIDTH/2+8,20,c,1,16,1);
            GUI_ShowString(WIDTH/2+8+9,20,".",16,1);
            GUI_ShowNum(WIDTH/2+8+16,20,d,1,16,1);

            GUI_ShowNum(WIDTH/2+8,47,f,1,16,1);
            GUI_ShowNum(WIDTH/2+8+8,47,g,1,16,1);
            GUI_ShowString(WIDTH/2+8+18,47,"/",16,1);
            GUI_ShowString(WIDTH/2+8+18+8,55,"rh",8,1);

            //显示当前时间
            GUI_ShowNum(1,1,year,4,8,1);
            GUI_ShowString(25,1,"-",8,18);
            GUI_ShowNum(31,1,month_shi,1,8,1);
            GUI_ShowNum(37,1,month_ge,1,8,1);
            GUI_ShowString(43,1,"-",8,18);
            GUI_ShowNum(49,1,day_shi,1,8,1);
            GUI_ShowNum(55,1,day_ge,1,8,1);

            GUI_ShowNum(76-4,1,hour_shi,1,8,1);
            GUI_ShowNum(82-4,1,hour_ge,1,8,1);
            GUI_ShowString(88-4,1,":",8,18);
            GUI_ShowNum(94-4,1,min_shi,1,8,1);
            GUI_ShowNum(100-4,1,min_ge,1,8,1);
            GUI_ShowString(106-4,1,":",8,18);
            GUI_ShowNum(112-4,1,sec_shi,1,8,1);
            GUI_ShowNum(118-4,1,sec_ge,1,8,1);

        }
        else 
            Warning_Menu();
    }
    sleep(2);
}

void Left_Menu(void)
{
    //显示左侧菜单
    if(Temperature < TEMP_MAX)
    {
        if(oled_flag == 1)
            Heating_Mode_Select();
        else if(oled_flag == 2)
            Heating_Mode_Working();
        else if(oled_flag == 3)
            Setting_Mode_Select();
        else if(oled_flag >= 4){
            Setting_Mode_Working();
        }
    }
    else
        sleep(TEMP_WAITTIME + 10);  //等待温度降下
    if(oled_flag < 4)
        delay_ms(500);
}

//左侧菜单_加热(默认)
void Heating_Mode_Select(void)
{
    GUI_FillRectangle(1,13, 35,HEIGHT-1, 0);
    GUI_FillRectangle(1,13, 35,33, 1);
    GUI_ShowCHinese(3,15,16,"加热",0);
    GUI_ShowCHinese(3,36,16,"设置",1);
}
//左侧菜单_加热开
void Heating_Mode_Working(void)
{
    GUI_FillRectangle(1,13, 35,HEIGHT-1, 0);
    GUI_ShowCHinese(3,15,16,"加热",1);
    GUI_FillRectangle(1,32, 35,HEIGHT-1, 0);
}
//左侧菜单_设置
void Setting_Mode_Select(void)
{
    GUI_FillRectangle(1,13, 35,HEIGHT-1, 0);
    GUI_ShowCHinese(3,15,16,"加热",1);
    GUI_FillRectangle(1,34, 35,54, 1);
    GUI_ShowCHinese(3,36,16,"设置",0);
}
//设置模式
void Setting_Mode_Working(void)
{

    int max = 0;
    int max1 = 0;
    int max2 = 0;
    max = TEMP_MAX;
    max1 = max / 10;
    max2 = max % 10;

    GUI_FillRectangle(39,1, WIDTH-1,10, 0);
    GUI_FillRectangle(1,1, 37,HEIGHT-1, 0);
    GUI_FillRectangle(39,45, WIDTH-1,HEIGHT-1, 0);

    GUI_ShowNum(WIDTH/2,20,max1,1,16,1);
    GUI_ShowNum(WIDTH/2+8,20,max2,1,16,1);
    GUI_ShowNum(WIDTH/2+8+16,20, 0, 1,16,1);
    GUI_DrawCircle(WIDTH-1-19, 25, 1,2);
    GUI_ShowString(WIDTH-1-14,20,"C",16,1);

    GUI_ShowString(45,11,"TEMP_MAX",8,1);
    GUI_DrawLine(38, 1, 38, HEIGHT-1, 1);
    if(oled_flag == 4){
        GUI_FillRectangle(1,1, 35,19, 1);
        GUI_ShowCHinese(11,2,16,"加",0);
        GUI_ShowCHinese(11,23,16,"减",1);
        GUI_ShowCHinese(3,44,16,"保存",1);
    }
    else if(oled_flag == 5){
        GUI_FillRectangle(1,20, 35,40, 1);
        GUI_ShowCHinese(11,2,16,"加",1);
        GUI_ShowCHinese(11,23,16,"减",0);
        GUI_ShowCHinese(3,44,16,"保存",1);
    }
    else if(oled_flag == 6){
        GUI_FillRectangle(1,41, 35,61, 1);
        GUI_ShowCHinese(11,2,16,"加",1);
        GUI_ShowCHinese(11,23,16,"减",1);
        GUI_ShowCHinese(3,44,16,"保存",0);
    }
    delay_ms(1000);
    GUI_FillRectangle(1,1, 35,HEIGHT-1, 0);
    GUI_FillRectangle(39,1, WIDTH-1,10, 0);

}

//报警界面
void Warning_Menu(void)
{
    //清屏，显示报警界面，并闪烁三次(3s)
    OLED_Clear(0);
    for(int j=1; j < 4; j++){
        delay_ms(500);
        GUI_DrawBMP(20,24,15,16,BMP_fire,1);    //火焰图标
        GUI_ShowCHinese(37,24,16,"温度过高",1);
        GUI_ShowString(103,24,"!",16,1);
        delay_ms(500);
        OLED_Clear(0);
    }
    GUI_ShowCHinese(18,24,16,"即将关闭加热",1);
    GUI_ShowString(116,24,"!",16,1);
    sleep(3);
    OLED_Clear(0);

    temp_waiting();
    //OLED_Clear(0);
}

//等待冷却界面，显示20s(默认冷却时间)的倒计时
void temp_waiting(void)
{
    OLED_Clear(0);
    GUI_ShowCHinese(18,20,16,"高温保护模式",1);
    for(int i=TEMP_WAITTIME; i>0; i--){
        GUI_ShowNum(WIDTH/2-8,40,i,2,16,1);
        GUI_ShowString(WIDTH/2+9,47,"s",8,1);
        sleep(1);
    }
    OLED_Clear(0);
}
